using System;
using System.Configuration;
using System.Web.Configuration;
using System.Web;
using System.IO;
using System.Web.UI.WebControls;

namespace Bbq.Gallery
{
    /// <summary>
    /// 
    /// </summary>
    public static class GalleryHelper
    {
        /// <summary>
        /// 
        /// </summary>
        public sealed class ThumbnailModes
        {
            public const string Item = "item";
            public const string Album = "album";
            public const string Download = "download";
        }

        public const string THUMBNAIL_MODE = "mode";
		public const string THUMBNAIL_IMAGETYPE = "it";

        public static string LastErrorDetail = string.Empty;

        /// <summary>
        /// Initializes the <see cref="Gallery"/> class.
        /// </summary>
        static GalleryHelper()
        {
            SetDefaultProvider();
        }

        private static object _initLock = new Object();
        internal static GalleryProvider _provider = null;

        /// <summary>
        /// Sets the default provider.
        /// </summary>
        private static void SetDefaultProvider()
        {
            lock (_initLock)
            {
                if (_provider != null) return;
                GalleryProviderConfigSection providerConfig = ConfigurationManager.GetSection("BbqGallery") as GalleryProviderConfigSection;
                if (providerConfig == null) return;
                ProviderSettings settings = providerConfig.Providers[providerConfig.DefaultProvider];
                _provider = ProvidersHelper.InstantiateProvider(settings, typeof(GalleryProvider)) as GalleryProvider;
                GalleryProvider.GalleryRootPath = providerConfig.GalleryRootPath;
                GalleryProvider.DefaultTemplate = providerConfig.DefaultTemplate;
                GalleryProvider.TemplatePath = providerConfig.TemplatePath;
                GalleryProvider.DefaultAlbumThumbnail = providerConfig.DefaultAlbumThumbnail;
                int pageSize = 0;
                if (int.TryParse(providerConfig.PageSize, out pageSize)) GalleryProvider.PageSize = pageSize;
                int thumbnailX = 132;
                if (int.TryParse(providerConfig.GalleryItemThumbnailX, out thumbnailX)) GalleryProvider.GalleryItemThumbnailX = thumbnailX;
                int thumbnailY = 99;
                if (int.TryParse(providerConfig.GalleryItemThumbnailY, out thumbnailY)) GalleryProvider.GalleryItemThumbnailY = thumbnailY;
                thumbnailX = 132;
                if (int.TryParse(providerConfig.GalleryAlbumThumbnailX, out thumbnailX)) GalleryProvider.GalleryAlbumThumbnailX = thumbnailX;
                thumbnailY = 99;
                if (int.TryParse(providerConfig.GalleryAlbumThumbnailY, out thumbnailY)) GalleryProvider.GalleryAlbumThumbnailY = thumbnailY;
                long thumbnailCompression = 70;
                if (long.TryParse(providerConfig.ThumbnailCompression, out thumbnailCompression)) GalleryProvider.ThumbnailCompression = thumbnailCompression;
                int pagerTotal = 10;
                if (int.TryParse(providerConfig.PagerTotal, out pagerTotal)) GalleryProvider.PagerTotal = pagerTotal;
                _dateTimeFormat = providerConfig.DateTimeFormat;
                if (string.IsNullOrEmpty(_dateTimeFormat)) _dateTimeFormat = "dd/MM/yyyy";
                //StorageType
                if (_provider is UrlPhysicalStorageGalleryProvider)
                {
                    UrlPhysicalStorageGalleryProvider urlProvider = _provider as UrlPhysicalStorageGalleryProvider;
                    UrlPhysicalStorageGalleryProvider.StorageTypes storageType = UrlPhysicalStorageGalleryProvider.StorageTypes.Mixed;
                    if (providerConfig.StorageType != null)
                    {
                        storageType = (UrlPhysicalStorageGalleryProvider.StorageTypes)Enum.Parse(
                            typeof(UrlPhysicalStorageGalleryProvider.StorageTypes), 
                            providerConfig.StorageType, true);
                    }
                    urlProvider.StorageType = storageType;
                }
            }
        }

        /// <summary>
        /// Gets the friendly measure string of size of the file.
        /// </summary>
        /// <param name="size">The size.</param>
        /// <returns></returns>
        public static string GetFriendlyFileSize(int size)
        {
            string sizeFormat = string.Concat("#", System.Globalization.CultureInfo.CurrentCulture.NumberFormat.CurrencyDecimalSeparator, "##");
            if (size <= 1) return string.Concat(size, " byte");
            if (size < 1000) return string.Concat(size, " bytes");
            float sizeF = ((float)size) / 1024; //KB
            if (sizeF < 1024) return string.Concat(sizeF.ToString(sizeFormat), " KB");
            sizeF = sizeF / 1024;   //MB
            //if (sizeF < 1024) 
            return string.Concat(sizeF.ToString(sizeFormat), " MB");
        }

        private static string _dateTimeFormat = null;
        /// <summary>
        /// Gets the date time format.
        /// </summary>
        /// <value>The date time format.</value>
        public static string DateTimeFormat
        {
            get { return _dateTimeFormat; }
        }

        /// <summary>
        /// Gets the pager total.
        /// </summary>
        /// <value>The pager total.</value>
        public static int PagerTotal
        {
            get { return GalleryProvider.PagerTotal; }
        }

        /// <summary>
        /// Gets the name of the handler.
        /// </summary>
        public const string ThumbnailHandlerName = "BbqThumbnailHandler.ashx";

        /// <summary>
        /// Gets the thumbnail compression.
        /// </summary>
        /// <value>The thumbnail compression.</value>
        public static long ThumbnailCompression
        {
            get { return GalleryProvider.ThumbnailCompression; }
        }

        /// <summary>
        /// Gets the gallery item thumbnail X.
        /// </summary>
        /// <value>The gallery item thumbnail X.</value>
        public static int GalleryItemThumbnailX
        {
            get { return GalleryProvider.GalleryItemThumbnailX; }
        }

        /// <summary>
        /// Gets the gallery item thumbnail Y.
        /// </summary>
        /// <value>The gallery item thumbnail Y.</value>
        public static int GalleryItemThumbnailY
        {
            get { return GalleryProvider.GalleryItemThumbnailY; }
        }

        /// <summary>
        /// Gets the gallery album thumbnail X.
        /// </summary>
        /// <value>The gallery album thumbnail X.</value>
        public static int GalleryAlbumThumbnailX
        {
            get { return GalleryProvider.GalleryAlbumThumbnailX; }
        }

        /// <summary>
        /// Gets the gallery album thumbnail Y.
        /// </summary>
        /// <value>The gallery album thumbnail Y.</value>
        public static int GalleryAlbumThumbnailY
        {
            get { return GalleryProvider.GalleryAlbumThumbnailY; }
        }

        /// <summary>
        /// Gets the gallery root path.
        /// </summary>
        /// <value>The gallery root path.</value>
        public static string GalleryRootPath
        {
            get { return GalleryProvider.GalleryRootPath; }
        }

        /// <summary>
        /// Gets the default template.
        /// </summary>
        /// <value>The default template.</value>
        public static string DefaultTemplate
        {
            get { return GalleryProvider.DefaultTemplate; }
        }

        /// <summary>
        /// Gets the template path.
        /// </summary>
        /// <value>The template path.</value>
        public static string TemplatePath
        {
            get { return GalleryProvider.TemplatePath; }
        }

        /// <summary>
        /// Gets the size of the page.
        /// </summary>
        /// <value>The size of the page.</value>
        public static int PageSize
        {
            get { return GalleryProvider.PageSize; }
        }

        /// <summary>
        /// Gets the default album thumbnail.
        /// </summary>
        /// <value>The default album thumbnail.</value>
        public static string DefaultAlbumThumbnail
        {
            get { return GalleryProvider.DefaultAlbumThumbnail; }
        }

        /// <summary>
        /// Determines whether [is root album] [the specified album].
        /// </summary>
        /// <param name="album">The album.</param>
        /// <returns>
        /// 	<c>true</c> if [is root album] [the specified album]; otherwise, <c>false</c>.
        /// </returns>
        public static bool IsRootAlbum(GalleryAlbum album)
        {
            return _provider.IsRootAlbum(album);
        }

        /// <summary>
        /// Creates the album.
        /// </summary>
        /// <param name="album">The album.</param>
        public static bool CreateAlbum(GalleryAlbum parent, GalleryAlbum album)
        {
            return _provider.CreateAlbum(parent, album);
        }

        /// <summary>
        /// Deletes the album.
        /// </summary>
        /// <param name="album">The album.</param>
        public static void DeleteAlbum(GalleryAlbum album)
        {
            _provider.DeleteAlbum(album);
        }

        /// <summary>
        /// Updates the album.
        /// </summary>
        /// <param name="album">The album.</param>
        public static void UpdateAlbum(GalleryAlbum album)
        {
            _provider.UpdateAlbum(album);
        }

        /// <summary>
        /// Gets the child albums.
        /// </summary>
        /// <param name="album">The album.</param>
        /// <returns></returns>
        public static GalleryAlbum[] GetChildAlbums(GalleryAlbum album)
        {
            return _provider.GetChildAlbums(album);
        }

        /// <summary>
        /// Gets the gallery items.
        /// </summary>
        /// <param name="album">The album.</param>
        /// <returns></returns>
        public static GalleryItem[] GetGalleryItems(GalleryAlbum album)
        {
            return _provider.GetGalleryItems(album);
        }

        /// <summary>
        /// Gets the gallery items count.
        /// </summary>
        /// <param name="album">The album.</param>
        /// <returns></returns>
        public static int GetGalleryItemsCount(GalleryAlbum album)
        {
            return _provider.GetGalleryItemsCount(album);
        }

        /// <summary>
        /// Gets the gallery album info.
        /// </summary>
        /// <param name="album">The album.</param>
        public static void GetGalleryAlbumInfo(GalleryAlbum album)
        {
            _provider.GetGalleryAlbumInfo(album);
        }

        /// <summary>
        /// Gets the gallery album info.
        /// </summary>
        /// <param name="albumID">The album ID.</param>
        /// <returns></returns>
        public static GalleryAlbum GetGalleryAlbumInfo(string albumID)
        {
            return _provider.GetGalleryAlbumInfo(albumID);
        }

        /// <summary>
        /// Resolves the album link.
        /// </summary>
        /// <param name="album">The album.</param>
        /// <returns></returns>
        public static string ResolveAlbumLink(GalleryAlbum album)
        {
            return _provider.ResolveAlbumLink(album);
        }

        /// <summary>
        /// Resolves the album thumbnail.
        /// </summary>
        /// <param name="album">The album.</param>
        /// <returns></returns>
        public static string ResolveAlbumThumbnail(GalleryAlbum album)
        {
            return _provider.ResolveAlbumThumbnail(album);
        }

        /// <summary>
        /// Resolves the item thumbnail.
        /// </summary>
        /// <param name="item">The item.</param>
        /// <returns></returns>
        public static string ResolveItemThumbnail(GalleryItem item)
        {
            return _provider.ResolveItemThumbnail(item);
        }

        /// <summary>
        /// Resolves the item download.
        /// </summary>
        /// <param name="item">The item.</param>
        /// <returns></returns>
        public static string ResolveItemDownload(GalleryItem item)
        {
            return _provider.ResolveItemDownload(item);
        }

        /// <summary>
        /// Resolves the item link.
        /// </summary>
        /// <param name="item">The item.</param>
        /// <returns></returns>
        public static string ResolveItemLink(GalleryItem item)
        {
            return _provider.ResolveItemLink(item);
        }

        /// <summary>
        /// Gets the physical path from the root folder of gallery.
        /// </summary>
        /// <param name="relativePathFromRoot">The relative path from root.</param>
        /// <returns></returns>
        public static string GetPhysicalPath(string relativePathFromRoot)
        {
            string path = string.Concat(GalleryHelper.GalleryRootPath, relativePathFromRoot);
            return System.Web.Hosting.HostingEnvironment.MapPath(path);
        }

        /// <summary>
        /// Return the physical path from the root folder of gallery.
        /// </summary>
        /// <returns></returns>
        public static string GetPhysicalPath()
        {
            return System.Web.Hosting.HostingEnvironment.MapPath(GalleryHelper.GalleryRootPath);
        }

        /// <summary>
        /// Gets the root.
        /// </summary>
        /// <value>The root.</value>
        public static GalleryAlbumNode Root
        {
            get { return _provider.Root; }
        }

        /// <summary>
        /// Creates the gallery item.
        /// </summary>
        /// <param name="item">The item.</param>
        public static bool CreateGalleryItem(GalleryAlbum parentAlbum, GalleryItem item, FileUpload fileUpload)
        {
            return _provider.CreateGalleryItem(parentAlbum, item, fileUpload, false);
        }

        /// <summary>
        /// Creates the gallery item.
        /// </summary>
        /// <param name="parentAlbum">The parent album.</param>
        /// <param name="item">The item.</param>
        /// <param name="fileUpload">The file upload.</param>
        /// <param name="isZipFile">if set to <c>true</c> [is zip file].</param>
        /// <returns></returns>
        public static bool CreateGalleryItem(GalleryAlbum parentAlbum, GalleryItem item, FileUpload fileUpload, bool isZipFile)
        {
            return _provider.CreateGalleryItem(parentAlbum, item, fileUpload, isZipFile);
        }

        /// <summary>
        /// Deletes the gallery item.
        /// </summary>
        /// <param name="item">The item.</param>
        public static void DeleteGalleryItem(GalleryItem item)
        {
            _provider.DeleteGalleryItem(item);
        }

        /// <summary>
        /// Gets the next gallery item.
        /// </summary>
        /// <param name="item">The item.</param>
        /// <returns></returns>
        public static GalleryItem GetNextGalleryItem(GalleryItem item)
        {
            return _provider.GetNextGalleryItem(item);
        }

        /// <summary>
        /// Gets the previous gallery item.
        /// </summary>
        /// <param name="item">The item.</param>
        /// <returns></returns>
        public static GalleryItem GetPreviousGalleryItem(GalleryItem item)
        {
            return _provider.GetPreviousGalleryItem(item);
        }

        /// <summary>
        /// Gets the gallery item info.
        /// </summary>
        /// <param name="item">The item.</param>
        /// <returns></returns>
        public static GalleryItem GetGalleryItemInfo(GalleryItem item)
        {
            return _provider.GetGalleryItemInfo(item);
        }

        /// <summary>
        /// Gets the gallery item info.
        /// </summary>
        /// <param name="itemID">The item ID.</param>
        /// <returns></returns>
        public static GalleryItem GetGalleryItemInfo(string itemID)
        {
            return _provider.GetGalleryItemInfo(itemID);
        }

        /// <summary>
        /// Gets the highlight item.
        /// </summary>
        /// <param name="album">The album.</param>
        /// <returns></returns>
        public static GalleryItem GetHighlightItem(GalleryAlbum album)
        {
            return _provider.GetHighlightItem(album);
        }

        /// <summary>
        /// Sets the highlight item.
        /// </summary>
        /// <param name="item">The item.</param>
        public static void SetHighlightItem(GalleryItem item)
        {
            _provider.SetHighlightItem( item);
        }

        /// <summary>
        /// Gets the path to album.
        /// </summary>
        /// <param name="album">The album.</param>
        /// <returns></returns>
        public static GalleryAlbum[] GetPathToAlbum(GalleryAlbum album)
        {
            return _provider.GetPathToAlbum(album);
        }

        /// <summary>
        /// Resolves the item identity.
        /// </summary>
        /// <param name="item">The item.</param>
        /// <returns></returns>
        public static string ResolveItemIdentity(GalleryItem item)
        {
            return _provider.ResolveItemIdentity(item);
        }

        /// <summary>
        /// Resolves the album identity.
        /// </summary>
        /// <param name="album">The album.</param>
        /// <returns></returns>
        public static string ResolveAlbumIdentity(GalleryAlbum album)
        {
            return _provider.ResolveAlbumIdentity(album);
        }
    }
}
